package com.u1s1.controller;

import com.sun.org.apache.xpath.internal.operations.Mod;
import com.sun.scenario.effect.impl.prism.PrImage;
import com.u1s1.entity.Users;
import com.u1s1.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import sun.security.krb5.internal.PAData;

import javax.jws.soap.SOAPBinding;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

@Controller
@RequestMapping(path = {"/udo"})
public class UsersController {
    @Autowired
    private UsersService usersService;

    @RequestMapping(path = {"/add"})
    private String add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Users user = new Users();
        user.setName(req.getParameter("username"));
        user.setPasword(req.getParameter("password"));
        user.setPhoneNumber(req.getParameter("phoneNo"));
        user.setAddress(req.getParameter("address"));
        user.setRegDate(new Date());

//        if (usersService.getCountByName(user.getName()) > 0) {
//            req.setAttribute("note", "用户名已存在");
//            req.getRequestDispatcher("/add.jsp").forward(req, resp);
//            return;
//        }

        int rows = usersService.insertSelective(user);
        if (rows > 0) {
            return "userInfo";
        } else {
            return "error";
        }
    }

    @RequestMapping(path = {"/query"})
    private String query(Model model) {

//        username.replaceAll("[`~!@#$^&*()=|{}':;',\\[\\].<>TRJdspfdsnvpod","");   我不知道正则应该怎么写来防止sql注入

        model.addAttribute("list", usersService.selectSelective(null));

        return "userInfo1";
    }

    @RequestMapping(path = {"/delete"})
    private String delete(int id) {
        int rows = usersService.deleteByPrimaryKey(id);
        System.out.println(rows);
        if (rows > 0) {
            return "redirect:/udo/query";
        } else {
            return "error";
        }
    }

    @RequestMapping(path = {"/update"})
    private String update(int id, Model model) throws ServletException, IOException {
        Users user = usersService.selectByPrimaryKey(id);
        model.addAttribute("user", user);
        return "update";
    }

    @RequestMapping(path = {"/updatedo"})
    private String updatedo(Users users, Model model) {
        int id = users.getId();
        //通过id把数据库中原来的user信息拿到
        Users oldUser = usersService.selectByPrimaryKey(id);

        String oldUsername = oldUser.getName();
        String newUsername = users.getName();

        List<Users> list = usersService.selectSelective(users);
        if (!newUsername.equals(oldUsername) && list.size() > 0) {
            model.addAttribute("note", "该用户名已存在或与原有用户名相同");
            return "forward:/udo/update";
        }

        int rows = usersService.updateByPrimaryKeySelective(users);

        if (rows > 0) {
            return "redirect:/udo/query";
        } else {
            return "redirect:error.jsp";
        }
    }

    @RequestMapping("/roomsquery")
    private String roomsquery(Model model) {
        Users users = new Users();
        List<Integer> list = new LinkedList<Integer>();
        for (int i = 100; i < 1000; i++) {
            String number = Integer.toString(i);
            users.setAddress(number);
            if (usersService.selectSelective(users).size() > 0) {
                list.add(1);
              //  System.out.println("=========="+i+list.get(i-100));
            } else {
                list.add(0);
            }
        }
        model.addAttribute("list", list);
        return "rooms";
    }
}
